#include <time.h>
#include <stdio.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include "nanorcs.h"

#define NOT_FOUND -1

using namespace std;

/* Returns the index of "-option" in argv. */
int GetIndex ( char *argv[], int argc, char option );

bool IsDirectory ( char *argument );

int main ( int argc, char *argv[] )
{ 
	Nanorcs nanorcs;	

	if ( argc < 2 )
	{
		cerr << "Not enough arguments\n";
		return EXIT_FAILURE;
	}	

	if ( strcmp ( argv[1], "checkin" ) == 0)
	{
		int rIndex= GetIndex ( argv, argc, 'r' );

		if ( rIndex == NOT_FOUND )
		{
			/* If the user didn't give -r option */
			
			nanorcs.CheckIn ( argv[2] );			
		}		
		else
		{
			/* If the user gave -r option */

			nanorcs.CheckIn ( argv[2], argv[3] );
		}			

	}
        else if ( strcmp ( argv[1], "checkout" ) == 0 )
        {
            if ( GetIndex ( argv, argc, 'r' ) !=  NOT_FOUND )
            {
                nanorcs.CheckOut( argv[3], version,  &argv[2][2] );
            }
            else if ( GetIndex ( argv, argc, 'd' ) != NOT_FOUND )
            {
                nanorcs.CheckOut( argv[3], date,  &argv[2][2] );
            }
            else if ( argc == 3 )
            {
                nanorcs.CheckOut(argv[2]);
            }
            else
            {
                cerr << "Wrong mode in check out\n";
                return 1;
            }
        }
        else if ( strcmp ( argv[1], "stable" ) == 0 )
        {    
            nanorcs.CheckOut ( argv[2], tag );
        }
        else if ( strcmp ( argv[1], "tag" ) == 0 )
        {
            nanorcs.TagProject ( argv[2] );
        }
        else if ( strcmp ( argv[1], "changes" ) == 0 )
        {
            if ( argc == 3 )
                nanorcs.Changes(argv[2]);
            else if ( argc == 4 )
                nanorcs.Changes( argv[3], argv[2] );
        }
}

/* Returns the index of "-option" in argv. */

int GetIndex ( char *argv[], int argc, char option )
{
	for ( int i = 2; i < argc; i++ )
	{
		if ( argv[i][0] == '-' && argv[i][1] == option )
		{
			return i;
		}
	}
	
	return NOT_FOUND;
}
